package app

import (
	"net/http"

	"server/pkg/errcode"

	"github.com/gin-gonic/gin"
)

type Response struct {
	c *gin.Context
}

func ToResponse(c *gin.Context, data interface{}) {
	if data == nil {
		data = gin.H{}
	}
	c.JSON(http.StatusOK, data)
}

func ToResponseList(c *gin.Context, data interface{}, total int64) {
	c.JSON(http.StatusOK, gin.H{
		"data":  data,
		"total": total,
	})
}

func ToErrorResponse(c *gin.Context, err *errcode.Error) {
	response := gin.H{"code": err.Code(), "msg": err.Msg()}

	details := err.Details()
	if len(details) > 0 {
		response["details"] = details
	}
	c.JSON(err.StatusCode(), response)
}
